package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import pojo.Binhluan;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.BeanUtil;
import util.MessageNotificationHepler;
import dao.BinhluanDAO;
import dao.SanphamDAO;

@ManagedBean(name = "trangChiTietSanPhamBean")
@ApplicationScoped
public class TrangChiTietSanPhamBean implements Serializable {

	private Taikhoan taiKhoan;

	private static final long serialVersionUID = 1L;
	private Sanpham currSanpham;
	private List<Sanpham> lstSanphamCungLoai;

	private List<Binhluan> lstBinhLuan;
	private String noiDungBinhLuan;

	@PostConstruct
	public void init() {
		// public TrangChiTietSanPhamBean()
		// {
		// LaySanPhamTheoMaSP();
		// LaySanPhamCungLoai();
		lstSanphamCungLoai = new ArrayList<Sanpham>();
	}

	public String ShowSanPham(Sanpham sp) {
		sp.setSoluotxem(sp.getSoluotxem() + 1);
		(new SanphamDAO()).saveOrUpdate(sp);
		currSanpham = sp;

		BinhluanDAO bldao = new BinhluanDAO();
		setLstBinhLuan(bldao
				.GetListBinhLuanConHoatDongTuList(new ArrayList<Binhluan>(sp
						.getBinhluans())));
		taiKhoan = ((TaiKhoanDangNhapBean) BeanUtil
				.findBean("taiKhoanDangNhapBean")).getTaiKhoan();
		LaySanPhamCungLoai();
		System.out.println("ShowSanPham: " + currSanpham.getTensanpham());
		System.out.println(currSanpham.getMasanpham());
		return "TrangChiTietSanPham?faces-redirect=true";
	}

	public void PostBinhLuan() {
		System.out.println("Post Binh Luan..!!" + getNoiDungBinhLuan());
		BinhluanDAO bldao = new BinhluanDAO();
		Binhluan bl = new Binhluan();

		bl.setDanhgia(1);
		bl.setDaduyet(true);
		bl.setDaxoa(false);
		bl.setNoidung(getNoiDungBinhLuan());
		bl.setNgayviet(Calendar.getInstance().getTime());
		bl.setSanpham(currSanpham);
		bl.setTaikhoan(getTaiKhoan());
		try {
			bldao.saveOrUpdate(bl);
		} catch (Exception ex) {
		}

		getLstBinhLuan().add(bl);
		setNoiDungBinhLuan(null);
		System.out.println("Complete BL..!!");

		MessageNotificationHepler
				.ShowMessage("Bình Luận đã được thêm thành công..!");
	}

	/*
	 * public void LaySanPhamTheoMaSP() { Map<String,String> params =
	 * FacesContext
	 * .getCurrentInstance().getExternalContext().getRequestParameterMap();
	 * String masp = params.get("masanpham");
	 * 
	 * SanphamDAO sanphamDAO = new SanphamDAO();
	 * 
	 * Sanpham sanpham = sanphamDAO.LaySanPhamTheoMaSP(Integer.parseInt(masp));
	 * 
	 * setCurrSanpham(sanpham);
	 * 
	 * System.out.println("currSanpham LaySanPhamTheoMaSP: " +
	 * currSanpham.getTensanpham()); }
	 */

	public Sanpham getCurrSanpham() {
		return currSanpham;
	}

	public void setCurrSanpham(Sanpham currSanpham) {
		this.currSanpham = currSanpham;
	}

	public List<Sanpham> getLstSanphamCungLoai() {
		return lstSanphamCungLoai;
	}

	public void setLstSanphamCungLoai(List<Sanpham> lstSanphamCungLoai) {
		this.lstSanphamCungLoai = lstSanphamCungLoai;
	}

	public void LaySanPhamCungLoai() {
		SanphamDAO sanphamDAO = new SanphamDAO();
		lstSanphamCungLoai = sanphamDAO.LaySanPhamCungLoai(currSanpham
				.getLoaisanpham().getMaloaisanpham(), 20);
	}

	public String getNoiDungBinhLuan() {
		return noiDungBinhLuan;
	}

	public void setNoiDungBinhLuan(String noiDungBinhLuan) {
		this.noiDungBinhLuan = noiDungBinhLuan;
	}

	public List<Binhluan> getLstBinhLuan() {
		return lstBinhLuan;
	}

	public void setLstBinhLuan(List<Binhluan> lstBinhLuan) {
		this.lstBinhLuan = lstBinhLuan;
	}

	public Taikhoan getTaiKhoan() {
		return taiKhoan;
	}

	public void setTaiKhoan(Taikhoan taiKhoan) {
		this.taiKhoan = taiKhoan;
	}
}
